//
//  ZCItemListVC.m
//  zctx
//
//  Created by 郭亚京 on 15/10/9.
//  Copyright © 2015年 sanliang. All rights reserved.
//

#import "ZCItemListVC.h"
#import "ZCItenCell.h"
#import "ZCItemDetailVC.h"
#import "ZCItemModelContent.h"
@interface ZCItemListVC ()<UITableViewDelegate, UITableViewDataSource,CelldetailsDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSIndexPath *selectindexPath;

@property (nonatomic,strong)NSMutableArray *titleArray;
@property (nonatomic,strong)NSMutableArray *contentIdArray;

@property (nonatomic,strong)UILabel *moneyLable;

@end

@implementation ZCItemListVC

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"ZCItemListVC"];
    [MTA trackPageViewBegin:@"ZCItemListVC"];
    self.tabBarController.hidesBottomBarWhenPushed = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"ZCItemListVC"];
    [MTA trackPageViewEnd:@"ZCItemListVC"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSLog(@"sdfhksdhfk = %ld",self.dataArray.count);
    [self setNavigationItems];
    self.contentIdArray = [ NSMutableArray array];
    self.titleArray = [NSMutableArray array];

    self.view.backgroundColor = [UIColor colorWithHexString:@"#ebeded"];
    self.navigationItem.title = @"选择检测项目";
    [self requestMainList];
    //添加tableview
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(0, 0,44,0));
    }];
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCItenCell" bundle:nil] forCellReuseIdentifier:@"itencell"];
    [self initbottomButton];
 
}
#pragma mark 确认按钮
-(void)initbottomButton{
    
    UIView *footView = [[UIView alloc]init];
    footView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:footView];
    [footView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).with.offset(0);
        make.right.equalTo(self.view).with.offset(0);
        make.height.mas_equalTo(@44);
        make.bottom.equalTo(self.view).with.offset(0);
    }];
    footView.layer.borderWidth =0.6;
    footView.layer.borderColor = RGB(236, 237, 238).CGColor;
    
    UILabel *jiancLable = [[UILabel alloc]init];
    [footView addSubview:jiancLable];
    [jiancLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(footView.left).with.offset(15);
        make.centerY.equalTo(footView.centerY).with.offset(0);
        make.height.mas_equalTo(@30);
        make.width.mas_equalTo(@(85));
    }];
    jiancLable.text =@"检测金额：";
    
    [footView addSubview:self.moneyLable];
    [self.moneyLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(jiancLable.right).with.offset(0);
        make.centerY.equalTo(footView.centerY).with.offset(0);
        make.height.mas_equalTo(@30);
        make.width.mas_equalTo(@(100));
    }];

    UIButton *okButton = [[UIButton alloc]init];
    [footView addSubview:okButton];
    [okButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(footView.right).with.offset(-15);
        make.centerY.equalTo(footView.centerY).with.offset(0);
        make.height.mas_equalTo(@35);
        make.width.mas_equalTo(@(100));
    }];
    [okButton addTarget:self action:@selector(ClickokButton) forControlEvents:UIControlEventTouchUpInside];
    [okButton setTitle:@"确认" forState:UIControlStateNormal];
    [okButton setBackgroundImage:[UIImage imageNamed:@"raise_confirm_bg"] forState:UIControlStateNormal];
}
#pragma mark - request

- (void)requestMainList {

    NSInteger muchMoney = 0;
    if (self.dataArray.count>0) {
        for (ZCItemModelContent *model in self.dataArray) {
            if (model.ischecked==1) {
                NSInteger money = [[NSString stringWithFormat:@"%@",model.price] integerValue];
                muchMoney += money;
            }
        }
        NSLog(@"--------- %ld",muchMoney);
        self.moneyLable.text = [NSString stringWithFormat:@"￥%ld元",muchMoney/100];
    }
}

#pragma mark - button

-(void)setNavigationItems {
    
    UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 19, 36);
    [leftButton setImage:[UIImage imageNamed:@"navigation_back"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * barButtonItem_leftNavBar = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = barButtonItem_leftNavBar;
}

- (void)backButtonPressed{
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UITableView DataSource & Delegate


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.dataArray count];

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIndentity = @"itencell";
    ZCItenCell *cell =(ZCItenCell *)[tableView dequeueReusableCellWithIdentifier:cellIndentity];
    if (!cell) {
        cell = [[ZCItenCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentity];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.delegate =self;
    cell.indexPath = indexPath;
    ZCItemModelContent *model =self.dataArray[indexPath.row];
    if (model.ischecked==1) {
        [cell setChecked:YES];
    }else{
        [cell setChecked:NO];
    }
    cell.nameLable.text = model.name;
    NSInteger money = [[NSString stringWithFormat:@"%@",model.price] integerValue];
    cell.moneyLable.text =[NSString stringWithFormat:@"%ld元",money/100];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ZCItenCell *cell = (ZCItenCell*)[tableView cellForRowAtIndexPath:indexPath];
    self.selectindexPath = indexPath;
    ZCItemModelContent *model =self.dataArray[indexPath.row];
    
    NSLog(@"model =%d ,row = %ld",model.checked,(long)indexPath.row);
    if (model.ischecked !=1) {
        model.ischecked = 1;
        [cell setChecked:YES];
    }else {
        model.ischecked = 0;
        [cell setChecked:NO];
    }
    NSInteger muchMoney = 0;
    for (ZCItemModelContent *model in self.dataArray) {
        
        if (model.ischecked ==1) {
            NSInteger money = [[NSString stringWithFormat:@"%@",model.price] integerValue];
            muchMoney += money;
        }
    }
    self.moneyLable.text = [NSString stringWithFormat:@"￥%ld元",muchMoney/100];
}


-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
}

#pragma mark 点击了详情
-(void)celldetailsPath:(NSIndexPath *)indexPath{

    ZCItemModelContent *model =self.dataArray[indexPath.row];
    ZCItemDetailVC *zcvc = [[ZCItemDetailVC alloc]init];
    zcvc.hidesBottomBarWhenPushed = YES;
    zcvc.model = model;
    [self.navigationController pushViewController:zcvc animated:YES];;
}

#pragma mark 
-(void)ClickokButton{
    
//    [listNameArray componentsJoinedByString:@","]; 将数组转成字符串用","分隔
    
    for (ZCItemModelContent *model in self.dataArray) {
        if (model.ischecked==1) {
            NSString * name =[NSString stringWithFormat:@"%@",model.name];
            NSString * contentIdentifier =[NSString stringWithFormat:@"%@",model.contentIdentifier];
            [self.titleArray addObject:name];
            [self.contentIdArray addObject:contentIdentifier];
        }
    }
    NSString *pid = [self.contentIdArray componentsJoinedByString:@","];
    NSString *pname = [self.titleArray componentsJoinedByString:@","];
    NSString *price = [NSString stringWithFormat:@"%@",self.moneyLable.text];
    
//    NSLog(@"pid = %@ , model = %@",pid,pname);
    self.cellselectItemList(pid,pname,price);
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - instance

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor colorWithHexString:@"#fafcfb"];
        _tableView.separatorColor = RGB(236, 237, 238);
        _tableView.tableFooterView = [[UIView alloc]init];
        
    }
    return _tableView;
}

-(UILabel *)moneyLable{
    if (!_moneyLable) {
        _moneyLable = [[UILabel alloc]init];
        _moneyLable.textColor = [UIColor redColor];
    }
    return _moneyLable;
}

@end
